package GUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;

import OBSZARY_def.MM;
import OBSZARY_def.ob;
import OBSZARY_def._obszar;
import OBSZARY_def._obszarR4;
import Root.DO_Start_AObrazek;
import Root._punktData;
import ZaznaczObszar.ZaznaczObszar;
import net.miginfocom.swing.MigLayout;

import java.awt.FlowLayout;
import java.awt.BorderLayout;

public class _PanelObszar extends JPanel{

	public _obszarR4 R4;
	public _obszarR4 _podobszar;
	private static final long serialVersionUID = 1L;

	public _PanelObszar(_obszarR4 R, _obszarR4 _podobszar) {
		this.R4 = R;
		this._podobszar = _podobszar;
		initWindow();
		addToWindow();
		_akualizujDane();
	}

	private JCheckBox chdzialam;	
	private JLabel lblNewLabel;
	private JButton btnRec;
	private JPanel panel;
	private JButton btnPonownieSzukaj;
	private JPanel panel_1;
	private JLabel _lblX;
	private JLabel _lblY;
	private JLabel _lblW;
	private JLabel _lblH;
	ZaznaczObszar test;
	private JButton btnAktualizujDane;
	private JPanel panel_3;
	
	public void initWindow() {
		setPreferredSize(new Dimension(430, 32));
		setMaximumSize(new Dimension(430, 20));
		setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		
		
		setLayout(new GridLayout(0, 5, 0, 0));
		lblNewLabel = new JLabel(R4.get_nazwaObszaru());
		lblNewLabel.setFont(new Font("Lucida Grande", Font.BOLD | Font.ITALIC, 13));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		add(lblNewLabel);
		
		btnRec = new JButton("Rectangle");
		btnRec.setToolTipText("mozesz zaznaczyc wlasne rectangle");
		if (R4.get_obszar() != null) btnRec.setForeground(Color.GREEN);
		else btnRec.setForeground(Color.RED);
		add(btnRec);
		btnRec.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Rectangle tmp = new Rectangle();
				new ZaznaczObszar(tmp, DO_Start_AObrazek._AktywnyObszarMonitora).Obszar();
				if (tmp.width!=0 && tmp.height!=0) R4.set_obszar(tmp);
				else R4.set_obszar(null);
				_akualizujDane();
			}
		});
		
		panel_3 = new JPanel();
		panel_3.setAlignmentY(0.0f);
		panel_3.setAlignmentX(0.0f);
		add(panel_3);
		panel_3.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
		
		chdzialam = new JCheckBox("dzialam");
		chdzialam.setPreferredSize(new Dimension(40, 30));
		panel_3.add(chdzialam);
		chdzialam.setIconTextGap(0);
		chdzialam.setHorizontalTextPosition(SwingConstants.CENTER);
		chdzialam.setName("");
		chdzialam.setVerticalTextPosition(SwingConstants.BOTTOM);
		chdzialam.setFont(new Font("Lucida Grande", Font.BOLD | Font.ITALIC, 7));
		chdzialam.setSelected(R4.is_flaga());
		chdzialam.setSelected(true);
		
		btnAktualizujDane = new JButton("AA");
		btnAktualizujDane.setHorizontalAlignment(SwingConstants.LEFT);
		btnAktualizujDane.setVerticalAlignment(SwingConstants.BOTTOM);
		panel_3.add(btnAktualizujDane);
		btnAktualizujDane.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				_akualizujDane();
			}
		});
		btnAktualizujDane.setToolTipText("Aktualizuj dane");
		btnAktualizujDane.setPreferredSize(new Dimension(35, 23));
		btnAktualizujDane.setMaximumSize(new Dimension(23, 23));
		btnAktualizujDane.setForeground(Color.ORANGE);
		btnAktualizujDane.setAlignmentY(0.0f);
		btnAktualizujDane.setActionCommand("");
		chdzialam.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(chdzialam.isSelected() && R4.get_obszar() != null) R4.set_flaga(true);
				else R4.set_flaga(false);
				System.out.println("flaga " + R4.get_nazwaObszaru() + " " + R4.is_flaga());
			}
		});
		
		panel = new JPanel();
		panel.setAlignmentY(Component.TOP_ALIGNMENT);
		panel.setAlignmentX(Component.LEFT_ALIGNMENT);
		add(panel);
		panel.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 3));
		
		btnPonownieSzukaj = new JButton("PS");
		btnPonownieSzukaj.setAlignmentY(Component.TOP_ALIGNMENT);
		panel.add(btnPonownieSzukaj);
		btnPonownieSzukaj.setVerticalAlignment(SwingConstants.BOTTOM);
		btnPonownieSzukaj.setHorizontalAlignment(SwingConstants.LEFT);
		btnPonownieSzukaj.setHorizontalTextPosition(SwingConstants.CENTER);
		btnPonownieSzukaj.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				R4.aktywujObszar();
				_akualizujDane();
			}
		});
		btnPonownieSzukaj.setToolTipText("ponownie szukaj elementów");
		btnPonownieSzukaj.setActionCommand("");
		btnPonownieSzukaj.setForeground(Color.ORANGE);
		btnPonownieSzukaj.setPreferredSize(new Dimension(35, 23));
		btnPonownieSzukaj.setMaximumSize(new Dimension(23, 23));
		
		panel_1 = new JPanel();
		panel_1.setAlignmentX(Component.LEFT_ALIGNMENT);
		panel_1.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		add(panel_1);
		panel_1.setLayout(new GridLayout(2, 2, 0, 0));
		
		_lblX = new JLabel("x");
		_lblX.setFont(new Font("Lucida Grande", Font.BOLD | Font.ITALIC, 9));

		panel_1.add(_lblX);
		
		_lblY = new JLabel("y");
		_lblY.setFont(new Font("Lucida Grande", Font.BOLD | Font.ITALIC, 9));
		panel_1.add(_lblY);
		
		_lblW = new JLabel("W");
		_lblW.setFont(new Font("Lucida Grande", Font.BOLD | Font.ITALIC, 9));
		panel_1.add(_lblW);
		
		_lblH = new JLabel("H");
		_lblH.setFont(new Font("Lucida Grande", Font.BOLD | Font.ITALIC, 9));
		panel_1.add(_lblH);
		
	}
	public void addToWindow(){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridwidth = 0;

		DO_Start_AObrazek._window.panel.add(this, gbc);
	}
	
	public void _akualizujDane(){
		try{
		_lblX.setText("x " + R4.get_obszar().x);
		}catch(NullPointerException e){_lblX.setText("x " + "-");}
		
		try{
		_lblY.setText("y " + R4.get_obszar().y);
		}catch(NullPointerException e){_lblY.setText("y " + "-");}
		
		try{
		_lblW.setText("W " + R4.get_obszar().width);
		}catch(NullPointerException e){_lblW.setText("W " + "-");}
		
		try{
		_lblH.setText("H " + R4.get_obszar().height);
		}catch(NullPointerException e){_lblH.setText("H " + "-");}
		
		if (R4.get_obszar() != null) btnRec.setForeground(Color.GREEN);
		else btnRec.setForeground(Color.RED);
		
		if (R4.get_obszar() != null) btnRec.setForeground(Color.GREEN);
		else btnRec.setForeground(Color.RED);
		
		if (R4.is_flaga()) chdzialam.setSelected(true);
		else  chdzialam.setSelected(false);
	}

}
